﻿using System;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json.Linq;

namespace FBEntities.FBObjects.User
{
    public class FBWork : IFBObject
    {
        public FBObject employer;
        public FBObject position;
        public string start_date;

        #region IFBObject Members

        public void Parse(JObject o)
        {
            foreach (FieldInfo field in GetType().GetFields())
            {
                if (o[field.Name] == null)
                    continue;

                if (field.FieldType.Equals(typeof (string)))
                    field.SetValue(this, (string) o[field.Name]);
                else if (field.FieldType.GetInterfaces().Count(i => i.Equals(typeof (IFBObject))) > 0)
                {
                    var obj = (IFBObject) Activator.CreateInstance(field.FieldType);
                    JObject data = JObject.Parse(o[field.Name].ToString());
                    obj.Parse(data);
                    field.SetValue(this, obj);
                }
            }
        }

        #endregion
    }
}